package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DBPool {
	
	private static DataSource ds=null;
	static
	{
		try{
			Context ctx=new InitialContext();
			if(ctx==null)
				System.out.println("No Context");
			//System.out.println("Initial...");
			ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
		}catch(NamingException e)
		{
			e.printStackTrace();
		}
	
	}
	public static Connection getConnection() throws SQLException
	{
		return ds.getConnection();
	}
	
	/*public void closeConnection(Connection con)
	{
		try{
			if(con!=null)
				con.close();
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}*/
	/*public DataSource()
	{
		try{
			Class.forName("com.mysql.jdbc.Driver");
		}catch(ClassNotFoundException e)
		{e.printStackTrace();}
	}
	public  Connection getConnection() throws SQLException
	{
		String url="jdbc:mysql://localhost:3306/chatroom";
		Connection con=java.sql.DriverManager.getConnection(url,"root",null);
		return con;
	}*/
	/*public static void main(String [] args)throws Exception
	{
		Connection con;
		Statement smt;
		ResultSet rs;
		String sql="select * from user ";
		DBPool ds=new DBPool();
		con=ds.getConnection();
		System.out.println("Connect to database...");
		smt=con.createStatement();
		rs=smt.executeQuery(sql);
		while(rs.next())
		{
			System.out.println("Username :"+rs.getString(2));
		}
		
		
	}*/
	
	
}
